'use strict';

swan_pc
    .factory('Organization', function ($resource, DateUtils, $cookieStore, localStorageService, $rootScope,$state) {
        var _currentOrganization = null;

        var Organization = $resource('api/organizations/:id', {}, {
            'query': {method: 'GET', isArray: true},
            'queryMy': {method: 'GET', isArray: true, url: 'api/my/organizations'},
            'queryProjects': {method: 'GET', isArray: true, url: 'api/organizations/:id/projects'},
            'queryTasks': {method: 'GET', isArray: true, url: 'api/organizations/:id/tasks'},
            'queryEvents': {method: 'GET', isArray: true, url: 'api/organizations/:id/events'},
            'queryCommunitys': {method: 'GET', isArray: true, url: 'api/organizations/:id/communities'},
            'get': {
                method: 'GET',
                transformResponse: function (data) {
                    data = angular.fromJson(data);
                    data.certDate = DateUtils.convertLocaleDateFromServer(data.certDate);
                    return data;
                }
            },
            'update': {
                method: 'PUT',
                transformRequest: function (data) {
                    data.certDate = DateUtils.convertLocaleDateToServer(data.certDate);
                    return angular.toJson(data);
                }
            },
            'save': {
                method: 'POST',
                transformRequest: function (data) {
                    data.certDate = DateUtils.convertLocaleDateToServer(data.certDate);
                    return angular.toJson(data);
                }
            }
        });
        Organization.setCurrentOrg = function (org) {
            _currentOrganization = org;
            localStorageService.set('_currentOrganization', org)
            $cookieStore.put('_currentOrganizationId', org ? org.id : null);
            $rootScope.$broadcast('swanApp:currentOrgChange', org);
            $state.go('overview');
        };
        Organization.currentOrg = function () {
            return localStorageService.get('_currentOrganization');
        };
        Organization.refreshOrg = function (data) {
            return Organization.queryMy().$promise.then(function (result) {
                $rootScope.organizations = result;
                var orgMap = {}
                for (var i = 0; i < $rootScope.organizations.length; i++) {
                    orgMap[$rootScope.organizations[i].id] = $rootScope.organizations[i];
                }
                if (Organization.currentOrg())
                    if (!orgMap[Organization.currentOrg().id])
                        Organization.setCurrentOrg(null);

                if (!Organization.currentOrg() && $rootScope.organizations.length > 0)
                    Organization.setCurrentOrg($rootScope.organizations[0]);
                if (!Organization.currentOrg()) {
                    $rootScope.$broadcast('swanApp:noOrg', data);
                }
                console.log("refresh org");
                return data;
            });
        }
        return Organization;
    });
